package com.mydesignpattern.beijing.responsibilitychain.middle;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by nevermore on 17/4/25.
 */
public class FilterChain implements Filter {

    //处理链
    public List<Filter> chain = new ArrayList<>();
    private int count;

    @Override
    public void doFilter(Request request, Response response, FilterChain filterChain) {
        if(count == chain.size()){
            return ;
        }
        Filter f = chain.get(count);
        count ++;
        f.doFilter(request, response, filterChain);
    }

    //添加filter的方法
    public FilterChain addFilter(Filter filter){
        this.chain.add(filter);
        return this;
    }

    //删除filter
    public FilterChain removeFilter(Filter filter){
        //todo
        //可以考虑重写各个filter实现类的tostring方法，如果tostring相同则从chain中移除
        return this;
    }

    public List<Filter> getChain() {
        return chain;
    }

    public void setChain(List<Filter> chain) {
        this.chain = chain;
    }


}
